package com.lgj.springboot_lgj.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.springboot_lgj.bean.basic.BaseUser;
import com.lgj.springboot_lgj.dao.basic.BaseUserMapper;
import com.lgj.springboot_lgj.dao.ext.ArticleExtMapper;
import com.lgj.springboot_lgj.dao.ext.BaseUserExtMapper;
import com.lgj.springboot_lgj.exception.CustomerException;
import com.lgj.springboot_lgj.service.IArticleService;
import com.lgj.springboot_lgj.service.IBaseUserService;
import com.lgj.springboot_lgj.utils.JwtUtil;
import com.lgj.springboot_lgj.utils.UserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * ll
 * @author LDD
 * @date 2022/6/17 20:35
 */
@Service
public class BaseUserServiceImpl implements IBaseUserService {
    @Autowired
    private BaseUserMapper mapper;
    @Autowired
    //自定义的映射接口
    private BaseUserExtMapper extMapper;
    @Autowired
    private ArticleExtMapper articleExtMapper;
    @Autowired
    private IArticleService articleService;
    @Override
    public String login(String username, String password) {
        //1.根据用户名访问数据库 比较是否存在username 是否密码正确，是否锁定 是否状态正确\
        BaseUser baseUser  = extMapper.getUsername(username);
        //1.判断用户名不存在
        if(baseUser == null) {
            throw new CustomerException("用户不存在");
        }
        if(!Objects.equals(password,baseUser.getPassword())){
            throw new CustomerException("密码错误");
        }
        if(baseUser.getStatus()!=0){
            throw new CustomerException("账号被锁定");
        }
        //获取用户id作为生成token字符串信息
        String id = String.valueOf(baseUser.getUserId());
        //需要在用户登录成功后，将用户的其他信息保存在token中，
        Map<String, Object> userInfo = new HashMap();
        userInfo.put("username",baseUser.getUsername());
        userInfo.put("realname",baseUser.getRealname());
        userInfo.put("role",baseUser.getRole());
        //经过逻辑判断，登录成功，返回token字符串
        return JwtUtil.sign(id,userInfo);
        }
    //新增用户信息
    @Override
    public void addUser(BaseUser baseUser){
        BaseUser telephone =extMapper.getUserTelephone(baseUser.getTelephone());
        if(telephone!=null){
            throw new CustomerException("电话号码已存在,请重新输入");
        }
        baseUser.setRegisterTime(new Date());
        baseUser.setStatus(0);
        //默认注册身份为普通用户
        baseUser.setRole(1);
        mapper.insert(baseUser);

    }
    //分页查询
    @Override
    public PageInfo<BaseUser> pageQuery(int pageNum, int pageSize, String username, Integer role) {
        //设置当前页与页面大小
        PageHelper.startPage(pageNum,pageSize);
        List<BaseUser> list = extMapper.findAllByBaseUser(username,role);
        PageInfo<BaseUser> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    //更新用户信息
    @Override
    public void updateUser(BaseUser baseUser) {
        // baseUser 从前端获取更新数据封装的对象
        BaseUser baseUser1 =mapper.selectByPrimaryKey(baseUser.getUserId());
       if(baseUser1==null){
           throw new CustomerException("用户名不存在");
       }
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();
       //获取当前登录用户
        Object role = userInfo.get("role");
        Integer role2=(Integer)role;
        if(role2==1){
            BaseUser LoginUser = extMapper.getUsername((String) userInfo.get("username"));
            Integer userId =LoginUser.getUserId();
            Integer userId2 = baseUser.getUserId();
//            userInfo.get("userId").equals(baseUser.getUserId())
            if(userId==userId2){
                mapper.updateByPrimaryKeySelective(baseUser);
            }
            else{
                throw new CustomerException("没有权限修改其他用户的信息！");
            }
        }
        else{
            mapper.updateByPrimaryKeySelective(baseUser);
        }

    }
    //修改用户头像
    @Override
    public void updateUserIcon(int id, String userIcon) {
        BaseUser baseUser = mapper.selectByPrimaryKey(id);
        if(baseUser==null){
            throw new CustomerException("用户不存在");
        }
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();
        //获取当前登录用户
        Object role = userInfo.get("role");
        Integer role2=(Integer)role;
        if(role2==1){
            BaseUser LoginUser2 = extMapper.getUsername((String) userInfo.get("username"));
            Integer userId =LoginUser2.getUserId();
            Integer userId2 = baseUser.getUserId();
            if(userId==userId2){
                extMapper.updateUserIcon(id,userIcon);
            }
            else{
                throw new CustomerException("没有权限修改其他用户的信息！");
            }
        }
        else{
            extMapper.updateUserIcon(id,userIcon);
        }
    }
    //通过id删除用户信息
    @Override
    public void deleteUserById(Integer id) {
        BaseUser baseUser = mapper.selectByPrimaryKey(id);
        if(baseUser==null){
            throw new CustomerException("用户不存在");
        }
        if(baseUser.getRole()==0){
            throw new CustomerException("管理员不能删除");
        }
        Map<String, Object> userInfo = UserInfoUtil.getUserInfo();//获取当前登录用户
        Object role = userInfo.get("role");
        Integer role2=(Integer)role;
        if(role2==1){
            throw new CustomerException("没有权限删除用户信息");
        }
        List<Integer> list = articleExtMapper.findArticleIdByUserId(id);
        if(list.size()!=0){
            for (int i = 0; i <list.size() ; i++) {
                articleService.deleteById(list.get(i));
            }
        }
            mapper.deleteByPrimaryKey(id);
    }
    //获取当前登录用户的基本信息。
    @Override
    public BaseUser currentUserInfo(String token) {
        //根据token获取用户的唯一标识
        String userId = JwtUtil.getUserId(token);
        BaseUser baseUser = mapper.selectByPrimaryKey(Integer.valueOf(userId));
        return baseUser;
    }

}

